package com.wutu.nbyjk8.util;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: Addons
 * @Date: 2022-02-22 18:04
 * @Desc: 时间工具类
 */


public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
        public static String YYYY = "yyyy";

        public static String YYYY_MM = "yyyy-MM";

        public static String YYYY_MM_DD = "yyyy-MM-dd";

        public static String HH_mm_ss = "HH:mm:ss";

        public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
        public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

        private static String[] parsePatterns = {"yyyyMMddHHmmss",
                "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
                "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
                "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

        /**
         * 获取当前Date型日期
         *
         * @return Date() 当前日期
         */
        public static Date getNowDate()
        {
                return new Date();
        }




        public static String getStartOfWeek() {
                Calendar calendar = Calendar.getInstance();
                // 定位到当前日期，并根据日历的首日（默认是星期日）调整
                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(calendar.getTime());
        }

        public static String getEndOfWeek() {
                Calendar calendar = Calendar.getInstance();
                // 定位到当前日期，并根据日历的首日（默认是星期日）调整
                calendar.setFirstDayOfWeek(Calendar.MONDAY);
                calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());

                // 将日期设置为本周最后一天（星期六）
                calendar.add(Calendar.DAY_OF_WEEK, 6);

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(calendar.getTime());
        }




        public static String getStartOfQuarter() {
                Calendar calendar = Calendar.getInstance();
                int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，因此加1

                // 计算当前季度的开始月份
                int startMonth = ((month - 1) / 3) * 3 + 1;

                calendar.set(Calendar.MONTH, startMonth - 1); // 设置到该季度的开始月份
                calendar.set(Calendar.DAY_OF_MONTH, 1); // 设置为该月的第一天

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(calendar.getTime());
        }

        public static String getEndOfQuarter() {
                Calendar calendar = Calendar.getInstance();
                int month = calendar.get(Calendar.MONTH) + 1; // 月份从0开始，因此加1

                // 计算当前季度的结束月份
                int endMonth = ((month - 1) / 3) * 3 + 3;

                calendar.set(Calendar.MONTH, endMonth - 1); // 设置到该季度的结束月份
                calendar.set(Calendar.DAY_OF_MONTH, 1); // 将日期设置为该月的第一天
                calendar.add(Calendar.MONTH, 1); // 向前移动一个月
                calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); // 设置为该月的最后一天

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                return sdf.format(calendar.getTime());
        }

        /**
         * 获取当前日期, 默认格式为yyyy-MM-dd
         *
         * @return String
         */
        public static String getDate()
        {
                return dateTimeNow(YYYY_MM_DD);
        }

        public static final String getTime()
        {
                return dateTimeNow(YYYYMMDDHHMMSS);
        }

        public static final String getNowHour()
        {
                return dateTimeNow("HH");
        }
        public static final String getNowMonth()
        {
                return dateTimeNow("MM");
        }

        public static final String dateTimeNow()
        {
                return dateTimeNow(YYYYMMDDHHMMSS);
        }

        public static final String dateTimeNow(final String format)
        {
                return parseDateToStr(format, new Date());
        }

        public static final String dateTime(final Date date)
        {
                return parseDateToStr(YYYY_MM_DD, date);
        }

        public static final String parseDateToStr(final String format, final Date date)
        {
                return new SimpleDateFormat(format).format(date);
        }

        public static final Date dateTime(final String format, final String ts)
        {
                try
                {
                        return new SimpleDateFormat(format).parse(ts);
                }
                catch (ParseException e)
                {
                        throw new RuntimeException(e);
                }
        }

        /**
         * 日期路径 即年/月/日 如2018/08/08
         */
        public static final String datePath()
        {
                Date now = new Date();
                return DateFormatUtils.format(now, "yyyy/MM/dd");
        }

        /**
         * 日期路径 即年/月/日 如20180808
         */
        public static final String dateTime()
        {
                Date now = new Date();
                return DateFormatUtils.format(now, "yyyyMMdd");
        }

        /**
         * 日期型字符串转化为日期 格式
         */
        public static Date parseDate(Object str)
        {
                if (str == null)
                {
                        return null;
                }
                try
                {
                        return parseDate(str.toString(), parsePatterns);
                }
                catch (ParseException e)
                {
                        return null;
                }
        }

        /**
         * 获取服务器启动时间
         */
        public static Date getServerStartDate()
        {
                long time = ManagementFactory.getRuntimeMXBean().getStartTime();
                return new Date(time);
        }

        /**
         * 计算两个时间差
         */
        public static String getDatePoor(Date endDate, Date nowDate)
        {
                long nd = 1000 * 24 * 60 * 60;
                long nh = 1000 * 60 * 60;
                long nm = 1000 * 60;
                // long ns = 1000;
                // 获得两个时间的毫秒时间差异
                long diff = endDate.getTime() - nowDate.getTime();
                // 计算差多少天
                long day = diff / nd;
                // 计算差多少小时
                long hour = diff % nd / nh;
                // 计算差多少分钟
                long min = diff % nd % nh / nm;
                // 计算差多少秒//输出结果
                // long sec = diff % nd % nh % nm / ns;
                return day + "天" + hour + "小时" + min + "分钟";
        }

        public static String getLastMonthEnd() {
                Date date = new Date();
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                c.add(Calendar.MONTH, -1);
                c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
                Date lastDateOfPrevMonth = c.getTime();
                return parseDateToStr(YYYY_MM_DD, lastDateOfPrevMonth);
        }

        public static String getPrevDay(Date now) {
                Calendar c = Calendar.getInstance();
                c.setTime(now);
                c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
                return parseDateToStr(YYYY_MM_DD, c.getTime());
        }

        public static String getPrevMouth(String date){
                Calendar c = Calendar.getInstance();
                c.setTime(parseDate(date));
                c.set(Calendar.MONTH,c.get(Calendar.MONTH)-1);
                return parseDateToStr(YYYY_MM,c.getTime());
        }

        public static String getNextMouth(String date){
                Calendar c = Calendar.getInstance();
                c.setTime(parseDate(date));
                c.set(Calendar.MONTH,c.get(Calendar.MONTH)+1);
                return parseDateToStr(YYYY_MM,c.getTime());
        }

        public static String getPrevYear(String date){
                Calendar c = Calendar.getInstance();
                c.setTime(parseDate(date));
                c.set(Calendar.YEAR,c.get(Calendar.YEAR)-1);
                return parseDateToStr(YYYY_MM,c.getTime());
        }

        public static String getPrevDay(String now) {
                Date nowDay = parseDate(now);
                return getPrevDay(nowDay);
        }

        public static String getNextDay(Date now) {
                Calendar c = Calendar.getInstance();
                c.setTime(now);
                c.set(Calendar.DATE, c.get(Calendar.DATE) + 1);
                return parseDateToStr(YYYY_MM_DD, c.getTime());
        }

        public static Date getNextDay(String now) {
                Date nowDay = parseDate(now);
                return parseDate(getNextDay(nowDay));
        }



        public static String getTimeFromDate(Date date) {
                String dateStr = parseDateToStr(YYYY_MM_DD_HH_MM_SS, date);
                return dateStr.split(" ")[1];
        }

        public static Date getOneDayLastTime(Date date) {
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                return c.getTime();
        }

        public static Date getOneDayLastTime(String date) {
                Calendar c = Calendar.getInstance();
                c.setTime(parseDate(date));
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                return c.getTime();
        }

        public static String getMonthFirstDay(){
                Calendar cal_1= Calendar.getInstance();//获取当前日期
                String day = parseDateToStr(YYYY_MM_DD, cal_1.getTime());
                System.out.println(day.substring(8,10));
                if(day.substring(8,10)=="01"){
                        return day;
                }else {
                        cal_1.add(Calendar.MONTH, 0);
                        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天

                        return parseDateToStr(YYYY_MM_DD, cal_1.getTime());
                }

        }

        public static long getTimes(Date date1, Date date2){

                return (date2.getTime()-date1.getTime())/(1000);
        }


        public static String getStringTime(Date date){


                return parseDateToStr(YYYY_MM_DD_HH_MM_SS,date);
        }

        public static Timestamp getCreateTimeOrUpdateTime(){

                return new Timestamp(new Date().getTime());
        }

        public static List<String> getDateStringListNow(int num) {
                String time = DateUtils.dateTimeNow("YYYY-MM");

                List<String> dateList = new ArrayList<>();

                for (int i = 0; i < num; i++) {
                        dateList.add(time);
                        time= DateUtils.getPrevMouth(time);
                }

                return dateList;
        }

        public static List<String> getDateStringListPre(List<String> list) {

                List<String> dateList = new ArrayList<>();

                for (String time : list) {

                        dateList.add(DateUtils.getPrevYear(time));
                }

                return dateList;
        }

        public static List<String> getDateStringList(String date) {

                List<String> dateList = new ArrayList<>();
                dateList.add(date);
                String time = getNextMouth(date);;
                while (!time.equals(dateTimeNow("yyyy-MM"))){
                        dateList.add(time);
                }
                dateList.add(dateTimeNow("yyyy-MM"));

                return dateList;
        }

        public static int getTimeDifference(String oldTime,String newTime){

                long oldNums = dateTime("yyyy-MM-dd HH:mm:ss",oldTime).getTime();
                long newNums = dateTime("yyyy-MM-dd HH:mm:ss",newTime).getTime();

                return (int) (newNums-oldNums)/1000;
        }

}
